package com.lg.concurrent.interrupt;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * 结束一个 没有sleep 没有join的线程
 * 他是会继续执行完的。
 */
public class InterruptNoAnyThing {

    static class T extends Thread{
        Object lock;
        boolean flag;
        T(Object lock,boolean flag){
            this.lock = lock;
            this.flag = flag;
        }
        @SneakyThrows
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " start");
                if (flag) {
                    TimeUnit.SECONDS.sleep(5);
                }
                System.out.println(Thread.currentThread().getName() + " reRunning");
                System.out.println(Thread.currentThread().getName() + " end");
            }
        }
    }
    @SneakyThrows
    public static void main(String[] args) {

        Object o = new Object();
        T t1 = new T(o,true);
        T t2 = new T(o,false);
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        t2.start();

        t2.interrupt();
        System.out.println("main over");

    }
}
